const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
exports.main = async (event, context, callback) => {
  try {
    const { phone } = event
    const db = cloud.database();
    const {data} = await db.collection('users')
      .where({phone})
      .field({ // 限制返回字段
        _id: true,
        _openid: true,
        phone: true,
        nickName: true,
        avatarUrl: true
      })
      .get(); // 补全方法调用括号

    if (data.length > 0) {
      return {
        code: 200,
        data: data[0] // 返回首个匹配用户
      };
    }

    return {
      code: 404,
      message: '该手机号未注册'
    };
  } catch (error) {
    console.log('报错：', error)
    return {
      code: 500,
      message: err.message
    };
  }
};